package cn.source.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.source.system.domain.GkGradeDoc;
import cn.source.system.domain.SchoolMajorYearAdmission;
import cn.source.system.domain.SchoolZyzYearAdmission;
import cn.source.system.service.IGkGradeDocService;
import cn.source.system.service.ISchoolMajorYearAdmissionService;
import cn.source.system.service.ISchoolZyzYearAdmissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.system.domain.SchoolPlan;
import cn.source.system.service.ISchoolPlanService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 学校招生计划Controller
 * 
 * @author sourcebyte.vip
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/system/schoolPlan")
public class SchoolPlanController extends BaseController
{
    @Autowired
    private ISchoolPlanService schoolPlanService;

    @Autowired
    private ISchoolMajorYearAdmissionService schoolMajorYearAdmissionService;

    @Autowired
    ISchoolZyzYearAdmissionService schoolZyzYearAdmissionService;

    @Autowired
    private IGkGradeDocService gkGradeDocService;

    /**
     * 查询学校招生计划列表
     */
    @PreAuthorize("@ss.hasPermi('system:schoolPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(SchoolPlan schoolPlan)
    {
        startPage();
        List<SchoolPlan> list = schoolPlanService.selectSchoolPlanList(schoolPlan);
        return getDataTable(list);
    }
    
    /**
     * 查询学校招生计划列表
     */
    @PreAuthorize("@ss.hasPermi('system:schoolPlanCheck:list')")
    @GetMapping("/checkList")
    public TableDataInfo checkList(SchoolPlan schoolPlan)
    {
        startPage();
        List<SchoolPlan> list = schoolPlanService.selectSchoolPlanCheckList(schoolPlan);
        return getDataTable(list);
    }

    /**
     * 导出学校招生计划列表
     */
    @PreAuthorize("@ss.hasPermi('system:schoolPlan:export')")
    @Log(title = "学校招生计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SchoolPlan schoolPlan)
    {
        List<SchoolPlan> list = schoolPlanService.selectSchoolPlanList(schoolPlan);
        ExcelUtil<SchoolPlan> util = new ExcelUtil<SchoolPlan>(SchoolPlan.class);
        util.exportExcel(response, list, "学校招生计划数据");
    }

    /**
     * 获取学校招生计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:schoolPlan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolPlanService.selectSchoolPlanById(id));
    }

    /**
     * 新增学校招生计划
     */
    @PreAuthorize("@ss.hasPermi('system:schoolPlan:add')")
    @Log(title = "学校招生计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolPlan schoolPlan)
    {

        String km2=schoolPlan.getKm2();
        if(km2!=null&&!km2.equals("不限")&&!km2.equals("")){
            if(km2.indexOf("+")>0){
                km2=km2.replace("+","、");
                String[] km2Arr=km2.split("、");
                schoolPlan.setKm21(km2Arr[0]);
                schoolPlan.setKm22(km2Arr[1]);
            }else{
                schoolPlan.setKm21(km2);
            }

        }
        int ret=schoolPlanService.insertSchoolPlan(schoolPlan);


        //查询该专业有多少个计划
        SchoolPlan schoolPlanQuery=new SchoolPlan();
        schoolPlanQuery.setMajorType(schoolPlan.getMajorType());
        schoolPlanQuery.setSchoolCode(schoolPlan.getSchoolCode());
        schoolPlanQuery.setZyz(schoolPlan.getZyz());
        schoolPlanQuery.setMajorCode(schoolPlan.getMajorCode());
        List<SchoolPlan> list2024=schoolPlanService.selectSchoolPlanList2(schoolPlanQuery);


        //查询该专业有多少个指南
        SchoolMajorYearAdmission majorYearAdmissionQuery=new SchoolMajorYearAdmission();
        majorYearAdmissionQuery.setMajorType(schoolPlan.getMajorType());
        majorYearAdmissionQuery.setsCode(schoolPlan.getSchoolCode());
        majorYearAdmissionQuery.setZyz(schoolPlan.getZyz());
        majorYearAdmissionQuery.setMajorCode(schoolPlan.getMajorCode());
        List<SchoolMajorYearAdmission> admissonList2023= schoolMajorYearAdmissionService.selectSchoolMajorYearAdmissionList(majorYearAdmissionQuery);


        List<SchoolPlan> addList=new ArrayList();

        //把指南同步至计划
        for(int i=0;i<admissonList2023.size();i++){
            SchoolMajorYearAdmission schoolMajorYearAdmission=admissonList2023.get(i);
            int year=schoolMajorYearAdmission.getYear();
            boolean planExist=false;

            String province="";
            for(int j=0;j<list2024.size();j++) {
                SchoolPlan schoolPlan2 = list2024.get(j);
                province = schoolPlan2.getSprovince();
            }

            List<GkGradeDoc> gradeList=null;
            if ("450000".equals(province)) {//广西院校，用广西排名

                //广西排位分
                GkGradeDoc gkGradeDoc = new GkGradeDoc();
                gkGradeDoc.setYear(year);
                if ("理工类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("工类");
                }
                if ("文史类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("史类");
                }
                gkGradeDoc.setUseIn("广西");
                gradeList = gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
            }else{
                //全国排位分
                GkGradeDoc gkGradeDoc=new GkGradeDoc();
                gkGradeDoc.setYear(year);
                if("理工类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("工类");
                }
                if("文史类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("史类");
                }
                gkGradeDoc.setUseIn("全国");
                gradeList=gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
            }

            for(int j=0;j<list2024.size();j++){
                SchoolPlan schoolPlan2=list2024.get(j);
                province=schoolPlan2.getSprovince();

                if(year==schoolPlan2.getYear().intValue()){
                    planExist=true;

                    schoolPlan.setMajorAdmin(schoolMajorYearAdmission.getAdmission());
                    schoolPlan.setMajorLowGrade(schoolMajorYearAdmission.getLowGrade());
                    schoolPlan.setMajorAvgGrade(schoolMajorYearAdmission.getAvgGrade());
                    if(schoolMajorYearAdmission.getLowGradeSeq()!=null){
                        schoolPlan.setMajorLowPm(schoolMajorYearAdmission.getLowGradeSeq());
                    }

                    Integer low = schoolMajorYearAdmission.getLowGrade();
                    if(low==null) low=0;
                    Integer avg = schoolMajorYearAdmission.getAvgGrade();
                    if(avg==null) avg=0;


                    for (int k = 0; k < gradeList.size(); k++) {
                        GkGradeDoc gradeDoc = gradeList.get(k);
                        if (gradeDoc.getGrade().intValue() == low.intValue()) {
                            if(schoolPlan.getMajorLowPm()==null) {
                                schoolPlan.setMajorLowPm(gradeDoc.getSeq());
                            }
                        }
                        if (gradeDoc.getGrade().intValue() == avg.intValue()) {
                            schoolPlan.setMajorAvgPm(gradeDoc.getSeq());
                        }
                    }


                    schoolPlanService.updateSchoolPlan(schoolPlan);

                    //计算专业组汇总数据
                    SchoolZyzYearAdmission query=new SchoolZyzYearAdmission();
                    query.setYear(year);
                    query.setMajorType(schoolPlan.getMajorType());
                    query.setZyz(schoolPlan.getZyz());
                    query.setsCode(schoolPlan.getSchoolCode());
                    List<SchoolZyzYearAdmission> zyzList = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(query);
                    if(zyzList!=null&&zyzList.size()>0){//有现成数据，修改即可
                        SchoolZyzYearAdmission zyzYearAdmission=zyzList.get(0);
                        schoolPlan.setZyzPlan(zyzYearAdmission.getPlan());
                        schoolPlan.setZyzLowPm(zyzYearAdmission.getLowGradeSeq());
                        schoolPlan.setZyzAdmin(zyzYearAdmission.getAdmission());
                        schoolPlan.setZyzLowGrade(zyzYearAdmission.getLowGrade());
                        schoolPlan.setZyzAvgGrade(zyzYearAdmission.getAvgGrade());

                        Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                        if(zyzAvg==null) zyzAvg=0;
                        for (int k = 0; k < gradeList.size(); k++) {
                            GkGradeDoc gradeDoc = gradeList.get(k);

                            if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                                schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                            }
                        }
                    }else{//没有现成数据，需要计算

                        List<SchoolPlan> listHz=schoolPlanService.selectZyzPlanList(schoolPlan);
                        if(listHz!=null&&listHz.size()>0){
                            SchoolPlan planHz=listHz.get(0);

                            schoolPlan.setZyzPlan(planHz.getZyzPlan());
                            //schoolPlan.setZyzLowPm(planHz.getLowGradeSeq());
                            schoolPlan.setZyzAdmin(planHz.getZyzAdmin());
                            schoolPlan.setZyzLowGrade(planHz.getZyzLowGrade());
                            schoolPlan.setZyzAvgGrade(planHz.getZyzAvgGrade());

                            Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                            Integer zyzLow =schoolPlan.getZyzLowGrade();
                            if(zyzAvg==null) zyzAvg=0;
                            if(zyzLow==null) zyzLow=0;
                            for (int k = 0; k < gradeList.size(); k++) {
                                GkGradeDoc gradeDoc = gradeList.get(k);

                                if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                                    schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                                }

                                if (gradeDoc.getGrade().intValue() == zyzLow.intValue()) {
                                    schoolPlan.setZyzLowPm(gradeDoc.getSeq());
                                }
                            }
                        }
                    }

                    schoolPlanService.updateSchoolPlan(schoolPlan);

                    break;

                }
            }
            //计划不存在，需要将指南数据插入计划
            if(!planExist){
                SchoolPlan p=new SchoolPlan();
                p.setYear(year);
                p.setMajorType(schoolPlan.getMajorType());

                p.setZyz(schoolPlan.getZyz());
                p.setSchoolCode(schoolPlan.getSchoolCode());
                p.setSchoolName(schoolPlan.getSchoolName());

                p.setMajorCode(schoolPlan.getMajorCode());
                p.setMajorName(schoolPlan.getMajorName());
                p.setMajorSeq(schoolPlan.getMajorSeq());

                p.setKm1(schoolPlan.getKm1());
                p.setKm2(schoolPlan.getKm2());
                p.setBatch("本科普通批");
                p.setKm21(schoolPlan.getKm21());
                p.setKm22(schoolPlan.getKm22());
                p.setKm23(schoolPlan.getKm23());
                p.setMajorPm(schoolPlan.getMajorPm());

                p.setMajorAdmin(schoolMajorYearAdmission.getAdmission());
                p.setMajorPlan(schoolMajorYearAdmission.getPlan());
                p.setMajorLowGrade(schoolMajorYearAdmission.getLowGrade());
                p.setMajorAvgGrade(schoolMajorYearAdmission.getAvgGrade());
                p.setMajorLowPm(schoolMajorYearAdmission.getLowGradeSeq());

                Integer low = schoolMajorYearAdmission.getLowGrade();
                if(low==null) low=0;
                Integer avg = schoolMajorYearAdmission.getAvgGrade();
                if(avg==null) avg=0;

                for (int k = 0; k < gradeList.size(); k++) {
                    GkGradeDoc gradeDoc = gradeList.get(k);
                    if (gradeDoc.getGrade().intValue() == low.intValue()) {
                        if(p.getMajorLowPm()==null) {
                            p.setMajorLowPm(gradeDoc.getSeq());
                        }
                    }
                    if (gradeDoc.getGrade().intValue() == avg.intValue()) {
                        p.setMajorAvgPm(gradeDoc.getSeq());
                    }
                }

                addList.add(p);
                schoolPlanService.insertSchoolPlanAllList2(addList);
                addList.clear();

                //计算专业组汇总数据
                SchoolZyzYearAdmission query=new SchoolZyzYearAdmission();
                query.setYear(year);
                query.setMajorType(schoolPlan.getMajorType());
                query.setZyz(schoolPlan.getZyz());
                query.setsCode(schoolPlan.getSchoolCode());
                List<SchoolZyzYearAdmission> zyzList = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(query);
                if(zyzList!=null&&zyzList.size()>0){//有现成数据，修改即可
                    SchoolZyzYearAdmission zyzYearAdmission=zyzList.get(0);
                    schoolPlan.setZyzPlan(zyzYearAdmission.getPlan());
                    schoolPlan.setZyzLowPm(zyzYearAdmission.getLowGradeSeq());
                    schoolPlan.setZyzAdmin(zyzYearAdmission.getAdmission());
                    schoolPlan.setZyzLowGrade(zyzYearAdmission.getLowGrade());
                    schoolPlan.setZyzAvgGrade(zyzYearAdmission.getAvgGrade());

                    Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                    if(zyzAvg==null) zyzAvg=0;
                    for (int k = 0; k < gradeList.size(); k++) {
                        GkGradeDoc gradeDoc = gradeList.get(k);

                        if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                            schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                        }
                    }
                }else{//没有现成数据，需要计算

                    List<SchoolPlan> listHz=schoolPlanService.selectZyzPlanList(schoolPlan);
                    if(listHz!=null&&listHz.size()>0){
                        SchoolPlan planHz=listHz.get(0);

                        schoolPlan.setZyzPlan(planHz.getZyzPlan());
                        //schoolPlan.setZyzLowPm(planHz.getLowGradeSeq());
                        schoolPlan.setZyzAdmin(planHz.getZyzAdmin());
                        schoolPlan.setZyzLowGrade(planHz.getZyzLowGrade());
                        schoolPlan.setZyzAvgGrade(planHz.getZyzAvgGrade());

                        Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                        Integer zyzLow =schoolPlan.getZyzLowGrade();
                        if(zyzAvg==null) zyzAvg=0;
                        if(zyzLow==null) zyzLow=0;
                        for (int k = 0; k < gradeList.size(); k++) {
                            GkGradeDoc gradeDoc = gradeList.get(k);

                            if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                                schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                            }

                            if (gradeDoc.getGrade().intValue() == zyzLow.intValue()) {
                                schoolPlan.setZyzLowPm(gradeDoc.getSeq());
                            }
                        }
                    }
                }

                schoolPlanService.updateSchoolPlan(schoolPlan);

            }
        }


        return toAjax(ret);
    }
    
    /**
     * 新增学校招生计划
     */
    @GetMapping(value = "/fixData")
    public AjaxResult fixData()
    {
    	SchoolPlan schoolPlan=new SchoolPlan();
    	schoolPlan.setYear(2024);
    	List<SchoolPlan> list = schoolPlanService.selectSchoolPlanList(schoolPlan);
    	for(int i=0;i<list.size();i++) {
    		SchoolPlan schoolPlan2=list.get(i);
    		String km2=schoolPlan2.getKm2();
    		if(km2!=null&&!km2.equals("")) {
	    		if(km2.indexOf("＋")>0) {
	    			String[] km2Arr=km2.split("＋");
	    			if(km2Arr[0].equals("思想政治")) {
	    				schoolPlan2.setKm21("政治");
	    			}else {
	    				schoolPlan2.setKm21(km2Arr[0]);
	    			}
	    			if(km2Arr.length>1) {
	    				if(km2Arr[1].equals("思想政治")) {
		    				schoolPlan2.setKm22("政治");
		    			}else {
		    				schoolPlan2.setKm22(km2Arr[1]);
		    			}
	    			}
	    			if(km2Arr.length>2) {
	    				if(km2Arr[2].equals("思想政治")) {
		    				schoolPlan2.setKm23("政治");
		    			}else {
		    				schoolPlan2.setKm23(km2Arr[2]);
		    			}
	    			}
	    			schoolPlanService.updateSchoolPlan(schoolPlan2);
	    		}else {
	    			if(km2.equals("不限")) {
	    				
	    			}else {
		    			if(km2.equals("思想政治")) {
		    				schoolPlan2.setKm21("政治");
		    			}else {
		    				schoolPlan2.setKm21(km2);
		    			}
		    			schoolPlanService.updateSchoolPlan(schoolPlan2);
	    			}
	    		}
	    		
    		}
    	}
        return toAjax(1);
    }

    /**
     * 修改学校招生计划
     */
    @PreAuthorize("@ss.hasPermi('system:schoolPlan:edit')")
    @Log(title = "学校招生计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolPlan schoolPlan)
    {

        int ret=schoolPlanService.updateSchoolPlan(schoolPlan);

        //查询该专业有多少个计划
        SchoolPlan schoolPlanQuery=new SchoolPlan();
        schoolPlanQuery.setMajorType(schoolPlan.getMajorType());
        schoolPlanQuery.setSchoolCode(schoolPlan.getSchoolCode());
        schoolPlanQuery.setZyz(schoolPlan.getZyz());
        schoolPlanQuery.setMajorCode(schoolPlan.getMajorCode());
        List<SchoolPlan> list2024=schoolPlanService.selectSchoolPlanList2(schoolPlanQuery);


        //查询该专业有多少个指南
        SchoolMajorYearAdmission majorYearAdmissionQuery=new SchoolMajorYearAdmission();
        majorYearAdmissionQuery.setMajorType(schoolPlan.getMajorType());
        majorYearAdmissionQuery.setsCode(schoolPlan.getSchoolCode());
        majorYearAdmissionQuery.setZyz(schoolPlan.getZyz());
        majorYearAdmissionQuery.setMajorCode(schoolPlan.getMajorCode());
        List<SchoolMajorYearAdmission> admissonList2023= schoolMajorYearAdmissionService.selectSchoolMajorYearAdmissionList(majorYearAdmissionQuery);

        List<SchoolPlan> addList=new ArrayList();

        //把指南同步至计划
        for(int i=0;i<admissonList2023.size();i++){
            SchoolMajorYearAdmission schoolMajorYearAdmission=admissonList2023.get(i);
            int year=schoolMajorYearAdmission.getYear();
            boolean planExist=false;

            String province="";
            for(int j=0;j<list2024.size();j++) {
                SchoolPlan schoolPlan2 = list2024.get(j);
                province = schoolPlan2.getSprovince();
            }

            List<GkGradeDoc> gradeList=null;
            if ("450000".equals(province)) {//广西院校，用广西排名

                //广西排位分
                GkGradeDoc gkGradeDoc = new GkGradeDoc();
                gkGradeDoc.setYear(year);
                if ("理工类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("工类");
                }
                if ("文史类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("史类");
                }
                gkGradeDoc.setUseIn("广西");
                gradeList = gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
            }else{
                //全国排位分
                GkGradeDoc gkGradeDoc=new GkGradeDoc();
                gkGradeDoc.setYear(year);
                if("理工类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("工类");
                }
                if("文史类".equals(schoolPlan.getMajorType())) {
                    gkGradeDoc.setGradeClass("史类");
                }
                gkGradeDoc.setUseIn("全国");
                gradeList=gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
            }

            for(int j=0;j<list2024.size();j++){
                SchoolPlan schoolPlan2=list2024.get(j);
                province=schoolPlan2.getSprovince();

                if(year==schoolPlan2.getYear().intValue()){
                    planExist=true;

                    schoolPlan.setMajorAdmin(schoolMajorYearAdmission.getAdmission());
                    schoolPlan.setMajorLowGrade(schoolMajorYearAdmission.getLowGrade());
                    schoolPlan.setMajorAvgGrade(schoolMajorYearAdmission.getAvgGrade());
                    if(schoolMajorYearAdmission.getLowGradeSeq()!=null){
                        schoolPlan.setMajorLowPm(schoolMajorYearAdmission.getLowGradeSeq());
                    }

                    Integer low = schoolMajorYearAdmission.getLowGrade();
                    if(low==null) low=0;
                    Integer avg = schoolMajorYearAdmission.getAvgGrade();
                    if(avg==null) avg=0;


                    for (int k = 0; k < gradeList.size(); k++) {
                        GkGradeDoc gradeDoc = gradeList.get(k);
                        if (gradeDoc.getGrade().intValue() == low.intValue()) {
                            if(schoolPlan.getMajorLowPm()==null) {
                                schoolPlan.setMajorLowPm(gradeDoc.getSeq());
                            }
                        }
                        if (gradeDoc.getGrade().intValue() == avg.intValue()) {
                            schoolPlan.setMajorAvgPm(gradeDoc.getSeq());
                        }
                    }


                    schoolPlanService.updateSchoolPlan(schoolPlan);

                    //计算专业组汇总数据
                    SchoolZyzYearAdmission query=new SchoolZyzYearAdmission();
                    query.setYear(year);
                    query.setMajorType(schoolPlan.getMajorType());
                    query.setZyz(schoolPlan.getZyz());
                    List<SchoolZyzYearAdmission> zyzList = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(query);
                    if(zyzList!=null&&zyzList.size()>0){//有现成数据，修改即可
                        SchoolZyzYearAdmission zyzYearAdmission=zyzList.get(0);
                        schoolPlan.setZyzPlan(zyzYearAdmission.getPlan());
                        schoolPlan.setZyzLowPm(zyzYearAdmission.getLowGradeSeq());
                        schoolPlan.setZyzAdmin(zyzYearAdmission.getAdmission());
                        schoolPlan.setZyzLowGrade(zyzYearAdmission.getLowGrade());
                        schoolPlan.setZyzAvgGrade(zyzYearAdmission.getAvgGrade());

                        Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                        if(zyzAvg==null) zyzAvg=0;
                        for (int k = 0; k < gradeList.size(); k++) {
                            GkGradeDoc gradeDoc = gradeList.get(k);

                            if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                                schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                            }
                        }
                    }else{//没有现成数据，需要计算

                        List<SchoolPlan> listHz=schoolPlanService.selectZyzPlanList(schoolPlan);
                        if(listHz!=null&&listHz.size()>0){
                            SchoolPlan planHz=listHz.get(0);

                            schoolPlan.setZyzPlan(planHz.getZyzPlan());
                            //schoolPlan.setZyzLowPm(planHz.getLowGradeSeq());
                            schoolPlan.setZyzAdmin(planHz.getZyzAdmin());
                            schoolPlan.setZyzLowGrade(planHz.getZyzLowGrade());
                            schoolPlan.setZyzAvgGrade(planHz.getZyzAvgGrade());

                            Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                            Integer zyzLow =schoolPlan.getZyzLowGrade();
                            if(zyzAvg==null) zyzAvg=0;
                            if(zyzLow==null) zyzLow=0;
                            for (int k = 0; k < gradeList.size(); k++) {
                                GkGradeDoc gradeDoc = gradeList.get(k);

                                if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                                    schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                                }

                                if (gradeDoc.getGrade().intValue() == zyzLow.intValue()) {
                                    schoolPlan.setZyzLowPm(gradeDoc.getSeq());
                                }
                            }
                        }
                    }

                    schoolPlanService.updateSchoolPlan(schoolPlan);

                    break;

                }
            }
            //计划不存在，需要将指南数据插入计划
            if(!planExist){
                SchoolPlan p=new SchoolPlan();
                p.setYear(year);
                p.setMajorType(schoolPlan.getMajorType());

                p.setZyz(schoolPlan.getZyz());
                p.setSchoolCode(schoolPlan.getSchoolCode());
                p.setSchoolName(schoolPlan.getSchoolName());

                p.setMajorCode(schoolPlan.getMajorCode());
                p.setMajorName(schoolPlan.getMajorName());
                p.setMajorSeq(schoolPlan.getMajorSeq());

                p.setKm1(schoolPlan.getKm1());
                p.setKm2(schoolPlan.getKm2());
                p.setBatch("本科普通批");
                p.setKm21(schoolPlan.getKm21());
                p.setKm22(schoolPlan.getKm22());
                p.setKm23(schoolPlan.getKm23());
                p.setMajorPm(schoolPlan.getMajorPm());

                p.setMajorAdmin(schoolMajorYearAdmission.getAdmission());
                p.setMajorPlan(schoolMajorYearAdmission.getPlan());
                p.setMajorLowGrade(schoolMajorYearAdmission.getLowGrade());
                p.setMajorAvgGrade(schoolMajorYearAdmission.getAvgGrade());
                p.setMajorLowPm(schoolMajorYearAdmission.getLowGradeSeq());

                Integer low = schoolMajorYearAdmission.getLowGrade();
                if(low==null) low=0;
                Integer avg = schoolMajorYearAdmission.getAvgGrade();
                if(avg==null) avg=0;

                for (int k = 0; k < gradeList.size(); k++) {
                    GkGradeDoc gradeDoc = gradeList.get(k);
                    if (gradeDoc.getGrade().intValue() == low.intValue()) {
                        if(p.getMajorLowPm()==null) {
                            p.setMajorLowPm(gradeDoc.getSeq());
                        }
                    }
                    if (gradeDoc.getGrade().intValue() == avg.intValue()) {
                        p.setMajorAvgPm(gradeDoc.getSeq());
                    }
                }

                addList.add(p);
                schoolPlanService.insertSchoolPlanAllList2(addList);
                addList.clear();

                //计算专业组汇总数据
                SchoolZyzYearAdmission query=new SchoolZyzYearAdmission();
                query.setYear(year);
                query.setMajorType(schoolPlan.getMajorType());
                query.setZyz(schoolPlan.getZyz());
                List<SchoolZyzYearAdmission> zyzList = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(query);
                if(zyzList!=null&&zyzList.size()>0){//有现成数据，修改即可
                    SchoolZyzYearAdmission zyzYearAdmission=zyzList.get(0);
                    schoolPlan.setZyzPlan(zyzYearAdmission.getPlan());
                    schoolPlan.setZyzLowPm(zyzYearAdmission.getLowGradeSeq());
                    schoolPlan.setZyzAdmin(zyzYearAdmission.getAdmission());
                    schoolPlan.setZyzLowGrade(zyzYearAdmission.getLowGrade());
                    schoolPlan.setZyzAvgGrade(zyzYearAdmission.getAvgGrade());

                    Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                    if(zyzAvg==null) zyzAvg=0;
                    for (int k = 0; k < gradeList.size(); k++) {
                        GkGradeDoc gradeDoc = gradeList.get(k);

                        if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                            schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                        }
                    }
                }else{//没有现成数据，需要计算

                    List<SchoolPlan> listHz=schoolPlanService.selectZyzPlanList(schoolPlan);
                    if(listHz!=null&&listHz.size()>0){
                        SchoolPlan planHz=listHz.get(0);

                        schoolPlan.setZyzPlan(planHz.getZyzPlan());
                        //schoolPlan.setZyzLowPm(planHz.getLowGradeSeq());
                        schoolPlan.setZyzAdmin(planHz.getZyzAdmin());
                        schoolPlan.setZyzLowGrade(planHz.getZyzLowGrade());
                        schoolPlan.setZyzAvgGrade(planHz.getZyzAvgGrade());

                        Integer zyzAvg = schoolPlan.getZyzAvgGrade();
                        Integer zyzLow =schoolPlan.getZyzLowGrade();
                        if(zyzAvg==null) zyzAvg=0;
                        if(zyzLow==null) zyzLow=0;
                        for (int k = 0; k < gradeList.size(); k++) {
                            GkGradeDoc gradeDoc = gradeList.get(k);

                            if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                                schoolPlan.setZyzAvgPm(gradeDoc.getSeq());
                            }

                            if (gradeDoc.getGrade().intValue() == zyzLow.intValue()) {
                                schoolPlan.setZyzLowPm(gradeDoc.getSeq());
                            }
                        }
                    }
                }

                schoolPlanService.updateSchoolPlan(schoolPlan);

            }
        }



        return toAjax(ret);
    }

    /**
     * 删除学校招生计划
     */
    @PreAuthorize("@ss.hasPermi('system:schoolPlan:remove')")
    @Log(title = "学校招生计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolPlanService.deleteSchoolPlanByIds(ids));
    }
}
